package entity

import (
	"errors"
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//COMP_JC_MX 托管中收导入
type COMP_JC_MX struct {
	ID          int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	RWID        int       `gorm:"column:RWID"`
	CUID        int       `gorm:"column:CUID"`
	JCLX        string    `gorm:"column:JCLX"`
	JCLXCaption string    `gorm:"-"`
	KHMC        string    `gorm:"column:KHMC"`
	ACNO        string    `gorm:"column:ACNO"`
	JE          float32   `gorm:"column:JE"`
	LL          float32   `gorm:"column:LL"`
	LSKSR       time.Time `gorm:"column:LSKSR"`
	LSJZR       time.Time `gorm:"column:LSJZR"`
	QX          float32   `gorm:"column:QX"`
	CB          float32   `gorm:"column:CB"`
	CB2         float32   `gorm:"column:CB2"`
	ZHCBL       float32   `gorm:"column:ZHCBL"`
}

//TableName 经营单位中收导入
func (COMP_JC_MX) TableName() string {
	return "T_COMP_JC_MX"
}

//BeforeSave 保存前的触发器
func (i *COMP_JC_MX) BeforeSave(tx *gorm.DB) (err error) {
	if i.LSKSR.After(i.LSJZR) {
		return errors.New("落实起始日不能大于落实截止日。")
	}
	var count int
	err = tx.Model(CompClie{}).Where("CUNM=?", i.KHMC).Count(&count).Error
	if err != nil {
		return err
	}
	if count == 0 {
		return errors.New("客户名称在系统中不存在，请输入正确的客户名称。")
	}
	sqlEx := "select ID from T_COMP_CLIE where CUNM=?"
	row := tx.Raw(sqlEx, i.KHMC).Row()
	row.Scan(&i.CUID)
	i.QX = (float32(i.LSJZR.Sub(i.LSKSR).Hours()/24) + 1) / 365
	if i.JCLX == "1" || i.JCLX == "2" || i.JCLX == "3" || i.JCLX == "4" {
		i.CB = i.JE * i.LL / 100 * float32(i.QX)
		i.CB2 = i.JE * float32(i.QX)
	} else {
		i.CB = i.JE
		i.CB2 = i.JE
	}
	if i.JCLX == "4" {
		var count int
		row := tx.Raw("select count(id) from V_COMP_JC where KHMC=?", i.KHMC).Row()
		err = row.Scan(&count)
		if err != nil {
			return
		}
		if count > 0 {
			sqlEx = "UPDATE T_COMP_JC SET BZ2=BZ2+'、'+? WHERE ID=?"
			err = tx.Exec(sqlEx, i.KHMC, i.RWID).Error
			if err != nil {
				return err
			}
		}
	}
	return
}

//AfterSave 保存后的触发器
func (i *COMP_JC_MX) AfterSave(tx *gorm.DB) (err error) {
	sqlEx := "EXEC UP_COMP_JC_MX ?"
	err = tx.Exec(sqlEx, i.RWID).Error
	if err != nil {
		return err
	}
	return
}

func (i *COMP_JC_MX) AfterDelete(tx *gorm.DB) (err error) {
	sqlEx := "EXEC UP_COMP_JC_MX ?"
	err = tx.Exec(sqlEx, i.RWID).Error
	if err != nil {
		return err
	}
	return nil
}

// AfterFind 查询后触发器
func (i *COMP_JC_MX) AfterFind(tx *gorm.DB) (err error) {
	if i.JCLX == "1" {
		i.JCLXCaption = "一般贷款日均"
	} else if i.JCLX == "2" {
		i.JCLXCaption = "贴现日均"
	} else if i.JCLX == "3" {
		i.JCLXCaption = "福费廷日均"
	} else if i.JCLX == "4" {
		i.JCLXCaption = "存款日均"
	} else if i.JCLX == "5" {
		i.JCLXCaption = "存款利润"
	} else if i.JCLX == "6" {
		i.JCLXCaption = "中收"
	}
	return
}

//COMP_JC_MXEF 返回结构体
func COMP_JC_MXEF() base.EntityFunc {
	return func() interface{} {
		entity := COMP_JC_MX{}
		return &entity
	}
}

//COMP_JC_MXESF 返回结构体数组
func COMP_JC_MXESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []COMP_JC_MX
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("COMP_JC_MX", base.NewDefaultDao(COMP_JC_MXEF(), COMP_JC_MXESF(), nil))
}
